// import CountUI from '../../views/Count'
import {connect} from 'react-redux'
import {createAddAction, createMinusAction,  createAddActionAsync} from '../../redux/actions/count'
import {Select, Button} from 'antd'
import React, {Component} from 'react'
// 定义UI组件
class CountUI extends Component {
  // 定义操作页面的方法 调用props中的方法操作store
  state = {selectValue: 1}
  add = () => {
    let {selectValue} = this.state
    this.props.add(selectValue*1)
  }
  minus = () => {
    let {selectValue} = this.state
    this.props.minus(selectValue*1)
  }
  addIffOdd = () => {
    let {selectValue} = this.state
    if (this.props.count % 2 !== 0) {
      this.props.add(selectValue*1)
    }
  }
  addAsync = () => {
    let {selectValue} = this.state
    this.props.addAsync(selectValue*1, 1000)
  }
  render() {
    return (
      <div>
        <h1>Count组件</h1>
        <h3>当前求和：{this.props.count}</h3>
        <h3>列表的人数：{this.props.persons.length}</h3>
        <Select defaultValue="1" onChange={(value) => {this.setState({selectValue:value})}}>
          <Select.Option value="1">1</Select.Option>
          <Select.Option value="2">2</Select.Option>
          <Select.Option value="3">3</Select.Option>
        </Select>
        <Button onClick={this.add}>+</Button>
        <Button onClick={this.minus}>-</Button>
        <Button onClick={this.addIffOdd}>当前求和为奇数再加</Button>
        <Button onClick={this.addAsync}>异步加</Button>
      </div>
    )
  }
}

// 生成一个容器组件：传入状态和操作状态的方法
const CountContainer = connect(
  // 原来是0 现在是一个对象allReducers
  state => ({count: state.count, persons: state.persons}),
  {
    add: createAddAction,
    minus: createMinusAction,
    addAsync: createAddActionAsync
  }
)(CountUI)
export default CountContainer